﻿//################################################################
//################################################################
//                   METHODS DEFINED
//###############################################################
//###############################################################
function contains(value, array) {
    for (var i = 0; i < array.length; i++) {
        if (value == array[i]) {
            return true;                       //create a function which will compare an item to what is stored in a particular array

        }
        else {
            if (i + 1 == array.length)          //if we have reached the end of the array then the value does not match anything in the array
            {
                return false;
            }
        }
    }

}
function findPlaces(locatePlaces) {

    if (markersArray.length > 0) {
        deleteOverlays();

    }
    var request =
    {
        location: map.getCenter(),
        radius: 500,
        types: [locatePlaces]
    };
    document.getElementById("textBox").value += "Got Here 1";


    var service = new google.maps.places.PlacesService(map);
    document.getElementById("textBox").value += "Got Here 2";
    service.nearbySearch(request, callback);
    document.getElementById("textBox").value += "Got Here 3";
}

function callback(results, status, pagination) {
    if (status != google.maps.places.PlacesServiceStatus.OK) {
        return;
    } else {
        createMarkers(results);


    }
}

function createMarkers(places) {
    var bounds = new google.maps.LatLngBounds();

    for (var i = 0, place; place = places[i]; i++) {
        var image = new google.maps.MarkerImage(
              place.icon, new google.maps.Size(71, 71),
              new google.maps.Point(0, 0), new google.maps.Point(17, 34),
              new google.maps.Size(25, 25));

        var marker = new google.maps.Marker({
            map: map,
            icon: image,
            title: place.name,
            position: place.geometry.location
        });


        markersArray.push(marker);
        bounds.extend(place.geometry.location);
    }
    map.fitBounds(bounds);
}

function deleteOverlays() {
    if (markersArray) {
        for (i in markersArray) {
            markersArray[i].setMap(null);
        }
        markersArray.length = 0;
    }
}



function accessDialog(text, heading) {
    $('#dialog').dialog('option', 'title', heading);
    document.getElementById("dialog").innerHTML = text;
    $("#dialog").dialog("open");

}
function modifyMapType(mapType) {
    map.setMapTypeId(mapType);

}


function processSVData(data, status) {
    if (status == google.maps.StreetViewStatus.OK) {
        map.setCenter(updateCoordinates);
        panorama.setPosition(updateCoordinates);
    }
    else {
        accessDialog("The Street View is not available for the location you have specified!!");

    }

}

function ZoomInOutMap(zoom) {
    map.setZoom(zoom);

}

function ModifyHeadingOrPitch(heading, pitch, zoom) {

    panorama.setPov
    ({
        heading: heading,
        pitch: pitch,
        zoom: zoom
    });

}

function ModifyLocation(updateCoordinates, heading, pitch) {
    document.getElementById("textBox").value += updateCoordinates;
    map.setCenter(updateCoordinates);
    panorama.setPosition(updateCoordinates);
    panorama.setPov
    ({
        heading: heading,
        pitch: pitch
    });

}
function goingForward() {

    if (lookPosition >= 0 && lookPosition < 90) {
        updateCoordinates = new google.maps.LatLng(latpoint + coOrdinateCalculations[0].AlterLat, lngpoint + coOrdinateCalculations[0].AlterLng);

    }
    else if (lookPosition >= 90 && lookPosition < 180) {
        updateCoordinates = new google.maps.LatLng(latpoint - coOrdinateCalculations[1].AlterLat, lngpoint + coOrdinateCalculations[1].AlterLng);
    }
    else if (lookPosition >= 180 && lookPosition < 270) {
        updateCoordinates = new google.maps.LatLng(latpoint - coOrdinateCalculations[0].AlterLat, lngpoint - coOrdinateCalculations[0].AlterLng);
    }
    else {
        updateCoordinates = new google.maps.LatLng(latpoint + coOrdinateCalculations[1].AlterLat, lngpoint - coOrdinateCalculations[1].AlterLng);
    }

}

function goingBack() {

    if (lookPosition >= 0 && lookPosition < 90) {
        updateCoordinates = new google.maps.LatLng(latpoint - coOrdinateCalculations[0].AlterLat, lngpoint - coOrdinateCalculations[0].AlterLng);

    }
    else if (lookPosition >= 90 && lookPosition < 180) {
        updateCoordinates = new google.maps.LatLng(latpoint + coOrdinateCalculations[1].AlterLat, lngpoint - coOrdinateCalculations[1].AlterLng);
    }
    else if (lookPosition >= 180 && lookPosition < 270) {
        updateCoordinates = new google.maps.LatLng(latpoint + coOrdinateCalculations[0].AlterLat, lngpoint + coOrdinateCalculations[0].AlterLng);
    }
    else {
        updateCoordinates = new google.maps.LatLng(latpoint - coOrdinateCalculations[1].AlterLat, lngpoint + coOrdinateCalculations[1].AlterLng);
    }

}
function turnLeft() {

    if (lookPosition >= 0 && lookPosition < 90) {
        updateCoordinates = new google.maps.LatLng(latpoint + coOrdinateCalculations[1].AlterLat, lngpoint - coOrdinateCalculations[1].AlterLng);

    }
    else if (lookPosition >= 90 && lookPosition < 180) {
        updateCoordinates = new google.maps.LatLng(latpoint + coOrdinateCalculations[0].AlterLat, lngpoint + coOrdinateCalculations[0].AlterLng);
    }
    else if (lookPosition >= 180 && lookPosition < 270) {
        updateCoordinates = new google.maps.LatLng(latpoint - coOrdinateCalculations[1].AlterLat, lngpoint + coOrdinateCalculations[1].AlterLng);
    }
    else {
        updateCoordinates = new google.maps.LatLng(latpoint - coOrdinateCalculations[0].AlterLat, lngpoint - coOrdinateCalculations[0].AlterLng);
    }

}
function turnRight() {

    if (lookPosition >= 0 && lookPosition < 90) {
        updateCoordinates = new google.maps.LatLng(latpoint - coOrdinateCalculations[1].AlterLat, lngpoint + coOrdinateCalculations[1].AlterLng);

    }
    else if (lookPosition >= 90 && lookPosition < 180) {
        updateCoordinates = new google.maps.LatLng(latpoint - coOrdinateCalculations[0].AlterLat, lngpoint - coOrdinateCalculations[0].AlterLng);
    }
    else if (lookPosition >= 180 && lookPosition < 270) {
        updateCoordinates = new google.maps.LatLng(latpoint + coOrdinateCalculations[1].AlterLat, lngpoint - coOrdinateCalculations[1].AlterLng);
    }
    else {
        updateCoordinates = new google.maps.LatLng(latpoint + coOrdinateCalculations[0].AlterLat, lngpoint + coOrdinateCalculations[0].AlterLng);
    }


}
























